Skip to content

arch/arm/stm32: fix the SPI issues found in the review of #22493 - #22721

Open
raiden00pl wants to merge 4 commits into
apache:masterfrom
raiden00pl:stm32-spi-followup
Open

raiden00pl wants to merge 4 commits into
apache:masterfrom
raiden00pl:stm32-spi-followup

Conversation

@raiden00pl

Copy link
Copy Markdown
Member

Summary

Fixes for the issues raised in the review of #22493:

  • SPI v3 slave: 9-16 bit frames did not work. The word size was read from
    CFG1.CRCSIZE, which never changes, so 16-bit frames used 8-bit DMA. The
    RX queue indexes were in bytes but used as DMA transfer counts and as word
    counts for SPIS_DEV_RECEIVE (RX DMA wrote 2 x QSIZE bytes into the queue).
  • SPI v3 slave: spi_enqueue() disabled the SPI to arm the TX DMA even while
    NSS was low, cutting the current frame. The TX DMA is now armed only
    while NSS is high, else from the NSS rising edge, as SPIS_CTRLR_ENQUEUE
    requires.
  • SPI v1v2: a transfer larger than SPIn_DMA_BUFFER was cut to the buffer
    size and the rest was dropped without error. It now falls back to
    non-DMA, as SPI v3 does.
  • STM32H7R/S: STM32_SPI_CLK_MAX was the 200 MHz kernel clock limit; DS14359
    gives 133 MHz as the master SCK maximum.

Impact

STM32 SPI slave on H7 (9-16 bit mode, TX enqueue), SPI v1v2 with
SPIn_DMA_BUFFER (F1/F2/F3/F4/G4/L1/F7/L4/L5/WB/WL5), H7R/S SPI clock limit.
No API or Kconfig change.

Testing

SPI v1v2 DMA buffer (NUCLEO-L496ZG + X-NUCLEO-EEPRMA1)

SPI1 DMA with SPI1_DMA_BUFFER=16. 256 random bytes written to each EEPROM
with dd bs=256, read back with dd bs=256 and dd bs=8, compared on the
host.

Before:

eeprom0: write256+read256: FAIL first diff at 16, 240 bytes differ | write256+read8: FAIL first diff at 16, 239 bytes differ | read256 vs read8: FAIL first diff at 16, 240 bytes differ
eeprom1: write256+read256: FAIL first diff at 16, 239 bytes differ | write256+read8: FAIL first diff at 16, 192 bytes differ | read256 vs read8: FAIL first diff at 16, 239 bytes differ
eeprom2: write256+read256: FAIL first diff at 16, 238 bytes differ | write256+read8: OK | read256 vs read8: FAIL first diff at 16, 238 bytes differ

After:

eeprom0: write256+read256: OK | write256+read8: OK | read256 vs read8: OK
eeprom1: write256+read256: OK | write256+read8: OK | read256 vs read8: OK
eeprom2: write256+read256: OK | write256+read8: OK | read256 vs read8: OK

SPI v3 slave (NUCLEO-H743ZI slave, NUCLEO-F446RE master)

SPI1 slave (/dev/spislv1, spislv), SPI1 master on the F446RE (spi).

16-bit frames (SPI_SLAVE_DRIVER_WIDTH=16), before:

nsh> spi exch -b 1 -w 16 -x 2 AABBCCDD
Sending:	BBAA DDCC 
Received:	0000 0000 

Data received from master (8 bytes): 00 00 00 00 00 00 00 00 

After:

nsh> spislv -x 4 11223344 -t 10
Slave: Queuing 4 bytes for sending to master: 11 22 33 44

nsh> spi exch -b 1 -w 16 -x 2 AABBCCDD
Sending:	BBAA DDCC 
Received:	2211 4433 

Data received from master (4 bytes): AA BB CC DD 

Enqueue while the master holds NSS low (F446RE PB6 driven low with mw),
H743 SPI1 CFG1 read over SWD. Before, TXDMAEN (bit 15) is set while NSS is
low, so the SPI was disabled during the frame:

CFG1 while NSS low : 0007C007
CFG1 after NSS high: 0007C007

After, the TX DMA is armed on the NSS rising edge:

CFG1 while NSS low : 00074007
CFG1 after NSS high: 0007C007

nsh> spi exch -b 1 -x 4 A1B2C3D4
Sending:	A1 B2 C3 D4 
Received:	55 66 77 88 

Data received from master (4 bytes): A1 B2 C3 D4 

8-bit transfers pass as before:

nsh> spi exch -b 1 -x 4 AABBCCDD
Sending:	AA BB CC DD 
Received:	11 22 33 44 

Data received from master (4 bytes): AA BB CC DD 

Not tested on hardware: the H7R/S SPI clock limit (build only).

The word size was taken from CFG1.CRCSIZE, which never changes, so
16-bit frames used 8-bit DMA.  The RX queue indexes were in bytes but
used as DMA transfer counts and as word counts for SPIS_DEV_RECEIVE.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: raiden00pl <raiden00@railab.me>
…ueue.

spi_enqueue() disabled the SPI to arm the TX DMA even while NSS was low,
cutting the current frame.  Arm the TX DMA only while NSS is high, else
from the NSS rising edge, as SPIS_CTRLR_ENQUEUE requires.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: raiden00pl <raiden00@railab.me>
A transfer larger than SPIn_DMA_BUFFER was cut to the buffer size and
the rest was dropped without error.  Fall back to non-DMA, as SPI v3.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: raiden00pl <raiden00@railab.me>
STM32_SPI_CLK_MAX was 200 MHz, which is the kernel clock limit, not the
SCK limit.  DS14359 gives 133 MHz as the master mode maximum.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: raiden00pl <raiden00@railab.me>
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: M The size of the change in this PR is medium labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants